# -*- coding: utf-8 -*-

"""
  @author: 唐黎琦
  @date: 2022/6/20 14:24
  @python version: 3.6 
  
  ---------------------------------------
  @desc: PyCharm => python设计模式实现
"""
from abc import ABC, abstractmethod


# 通过一个手机的例子来实现建造者模式的讲解
# ============================  实现要返回的对象  ============================
class Phone:
    def __init__(self, screen=None, battery=None, cpu=None):
        self.screen = screen
        self.battery = battery
        self.cpu = cpu

    def __repr__(self):
        return f"A phone with {self.screen} / {self.battery} / {self.cpu}"


# ============================  实现建造者抽象类  ============================
# 一台手机的生产需要电池、屏幕、处理器等等一系列步骤，并且有一定的流水线顺序，前后不可以乱

class PhoneBuilder(ABC):
    phone = Phone()

    @abstractmethod
    def build_screen(self):
        pass

    @abstractmethod
    def build_battery(self):
        pass

    @abstractmethod
    def build_cpu(self):
        pass


# ============================  实现建造者实现类  ============================
# 实现一个华为手机和苹果手机的建造者实现类

class IphoneBuilder(PhoneBuilder):
    def build_screen(self):
        self.phone.screen = "三星屏幕"

    def build_battery(self):
        self.phone.battery = "苹果电池"

    def build_cpu(self):
        self.phone.cpu = "A15芯片"


class HuaweiPhoneBuilder(PhoneBuilder):
    def build_screen(self):
        self.phone.screen = "京东方屏幕"

    def build_battery(self):
        self.phone.battery = "华为电池"

    def build_cpu(self):
        self.phone.cpu = "麒麟990芯片"


# ============================  实现指挥者  ============================
class Make:
    def __init__(self):
        pass

    @staticmethod
    def build(builder: PhoneBuilder):
        """
        必须满足先组装cpu,再到电池,再到屏幕这样的顺序
        """
        builder.build_cpu()
        builder.build_battery()
        builder.build_screen()
        print(builder.phone)
        return


Make.build(IphoneBuilder())
Make.build(HuaweiPhoneBuilder())
